package com.imessage.action.device.client;

import com.bean.BusinessWuma;
import com.bean.DeviceClient;
import com.imessage.comm.Constants;
import com.imessage.type.BusinessWumaState;
import com.imessage.type.DeviceClientState;
import com.imessage.vo.ClientVo;
import com.imessage.vo.WumaVo;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户
 */
@Slf4j
@RestController
@RequestMapping("/admin/client")
public class ClientAction extends ClientBaseAction {

    /**
     * 客户列表数据和搜索
     */
    @RequestMapping(value = "")
    public Object run(ClientVo clientVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, clientVo);
        Page<DeviceClient> page = baseService.findPage(
                DeviceClient.class, expressionList, null,
                getOrder(clientVo.getOrders()), clientVo.getPageCurrent(),
                clientVo.getPageSize());
        return page;
    }

    /**
     * 添加修改界面
     */
    @RequestMapping(value = "/initpage")
    public ModelAndView initpage( @RequestParam(name = "req_id")String id) {
        ModelAndView modelAndView = new ModelAndView();
        DeviceClient client = new DeviceClient();
        if (StringUtils.isNotBlank(id)) {
            client = baseService.findById(DeviceClient.class, id);
            modelAndView.setViewName("admin/device/client/modify");
        } else {
            modelAndView.setViewName("admin/device/client/add");
        }
        setModelAndView(modelAndView, client);
        return modelAndView;
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/add")
    public Object add(ClientVo clientVo) {
        DeviceClient client = new DeviceClient();
        setClient(clientVo, client);
        int state = baseService.saveObject(client);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/modify")
    public Object modify(ClientVo clientVo) {
        DeviceClient client = baseService.findById(DeviceClient.class, clientVo.getId());
        setClient(clientVo, client);
        int state = baseService.updateObject(client);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return client;
        }
        return getJsonForErr("", datagrid);
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del")
    public Object del(
            @RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            DeviceClient client = baseService.findById(DeviceClient.class, id);
            state = baseService.delObject(client);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }
}
